//
//  AppDelegate.swift
//  SFWeiboPro
//
//  Created by happy on 2017/8/31.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setupAddition()
        
        window = UIWindow()
        
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = SFTabBarViewController()
        
        window?.makeKeyAndVisible()
        
        //异步加载网络数据
//        loadAppInfo()
        
        return true
    }

}

extension AppDelegate {
    
    func setupAddition() {
    
        SVProgressHUD.setMinimumDismissTimeInterval(2.0)
        
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //#available 是检测设备的版本
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (success, error) in
                print("授权" + (success ? "成功":"失败"))
            }
        } else {
            
            let notifySettings = UIUserNotificationSettings(types: [.alert,.badge, .sound,], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
    
        }

    }
    
}

extension AppDelegate {
    
    func loadAppInfo() {
        
        //异步加载
        DispatchQueue.global().async { 
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            //写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用数据加载完毕...\(jsonPath)")
        }
        
    }
    
}
